# -*- coding: utf-8 -*-
"""
修复版API测试
"""
from api_keys_complete import JUHE_FLIGHT_APPKEY, JUHE_TRAIN_APPKEY
import requests
import json
from datetime import datetime, timedelta

print('🔍 测试修复版聚合数据API...')

# 测试机票API - 修复日期格式
print('\n📡 测试机票API（修复日期格式）...')
flight_url = 'https://apis.juhe.cn/flight/query'
flight_params = {
    'key': JUHE_FLIGHT_APPKEY,
    'dtype': 'json',
    'from': 'DLC',
    'to': 'HFE',
    'date': '2025-09-20'  # 使用正确的日期格式
}

try:
    response = requests.get(flight_url, params=flight_params, timeout=10)
    print(f'状态码: {response.status_code}')
    data = response.json()
    print(f'响应: {json.dumps(data, ensure_ascii=False)[:300]}...')
    
    if data.get('error_code') == 0:
        print('✅ 机票API正常')
        result = data.get('result', {})
        flights = result.get('data', [])
        print(f'找到 {len(flights)} 条航班数据')
    else:
        print(f'❌ 机票API错误: {data.get("reason", "未知")}')
        print(f'错误代码: {data.get("error_code")}')
except Exception as e:
    print(f'❌ 机票API异常: {e}')

# 测试火车票API - 使用标准车站名
print('\n📡 测试火车票API（使用标准车站名）...')
train_url = 'https://apis.juhe.cn/fapigw/train/query'
train_params = {
    'key': JUHE_TRAIN_APPKEY,
    'dtype': 'json',
    'from': '大连北',  # 使用12306标准车站名
    'to': '合肥南',    # 使用12306标准车站名
    'date': '2025-09-20'
}

try:
    response = requests.get(train_url, params=train_params, timeout=10)
    print(f'状态码: {response.status_code}')
    data = response.json()
    print(f'响应: {json.dumps(data, ensure_ascii=False)[:300]}...')
    
    if data.get('error_code') == 0:
        print('✅ 火车票API正常')
        result = data.get('result', {})
        trains = result.get('data', [])
        print(f'找到 {len(trains)} 条火车数据')
    else:
        print(f'❌ 火车票API错误: {data.get("reason", "未知")}')
        print(f'错误代码: {data.get("error_code")}')
except Exception as e:
    print(f'❌ 火车票API异常: {e}')

print('\n🔍 测试完成')